import { ref, watch } from "vue";

export const token = ref<string>(uni.getStorageSync("token") || "");
export const userInfo = ref<{
  gender?: number | null;
  id?: string;
  teacherEmail?: string;
  teacherName?: string;
  teacherPhone?: string;
}>(uni.getStorageSync("userInfo") || {});
watch(
  token,
  (newToken) => {
    uni.setStorageSync("token", newToken || "");
  },
  { flush: "sync" }
);
watch(
  userInfo,
  (info) => {
    uni.setStorageSync("userInfo", info || {});
  },
  { flush: "sync" }
);

// 退出登录
export function logout() {
  token.value = "";
  userInfo.value = {};

  const { route } = getCurrentPages().at(-1);
  if (route === "pages/login/index") return;
  return uni.reLaunch({
    url: "/pages/login/index",
  });
}
